package com.example.controller;

import com.example.common.Result;
import com.example.pojo.Account;
import com.example.pojo.Admin;
import com.example.pojo.Comment;
import com.example.pojo.LoginResponse;
import com.example.service.AdminService;
import com.example.service.CommentService;
import com.example.util.ThreadLocalUtil;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private CommentService commentService;


    /**
     * 添加评论
     * @param comment
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Comment comment) {
        commentService.add(comment);
            return Result.success();
    }



    /**
     * 删除评论
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteById(@RequestParam Integer id) {
        commentService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除评论
     * @return
     */
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        commentService.deleteBatch(ids);
        return Result.success();
    }


    /**@PathVariable的查询方式/selectById/1(/..)
     * 根据id查询用户
     * @return
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Comment comment = commentService.selectById(id);
        return Result.success(comment);
    }

    /**
     * 查询房间的所有评论
     *
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll(@RequestHeader("Authorization") String token,@RequestParam(required = false) String keyword,
                            @RequestParam(required = false) Integer roomId) {
        List<Comment> list = commentService.selectAll(token,keyword,roomId);

        return Result.success(list);
    }
    /**
     * 计算房间的评论
     */
    @GetMapping("/count")
    public Result countByRoomId(Integer roomId) {
        int count = commentService.countByRoomId(roomId);
        return Result.success(count);
    }


    /**@RequestParam的查询方式/selectPage?pageNum=1(&...=...)
     * @RequestParam(defaultValue = "1")默认值为1
     *@RequestParam(required = false)不是必须要传参
     * 分页查询
     */
    @GetMapping("/SelectPage")
    public Result selectPage(@RequestHeader("Authorization") String token,@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize, @RequestParam(required = false) String keyword,
                             @RequestParam(required = false) Integer roomId) {
        PageInfo<Comment> pageInfo= commentService.selectPage(token,pageNum,pageSize,keyword,roomId);
        return Result.success(pageInfo);
    }

}
